'''
Created on 2010-5-23

@author: Zizon
'''
import binascii
import hashlib
from hmac import HMAC
from urllib import quote as _quote , unquote_plus as _unquote

def unquote(s):
    return _unquote(s)

def quote(s):
    return _quote(s, '')

class Oauth(object):
    
    @classmethod
    def signingRequest(self, method, url, parameters , comsumer_secret , token_secret , hash=hashlib.sha1):
        return  binascii.b2a_base64(
                                        HMAC(
                                             "%s&%s" % (quote(comsumer_secret) , quote(token_secret)),
                                             "&".join(
                                                         [ 
                                                              method.upper() ,
                                                              quote(url.lower()) ,
                                                              "%26".join([ quote("%s=%s" % (quote(key) , quote(value))) for key , value in sorted(parameters.items())])
                                                          ]
                                                      ),
                                              hash
                                        ).digest()
                                    )[:-1] 
        
